
package com.viweb.storemanager.temp;

import java.awt.Cursor;
import java.awt.Insets;
import java.awt.Shape;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.geom.Ellipse2D;
import javax.swing.ImageIcon;
import javax.swing.JButton;

public class RoundButton extends JButton implements MouseListener {

    private ImageIcon icon,  selectedIcon,  rolloverIcon;

    /**
     * Create a round button
     *
     */
    public RoundButton() {
        super();
        this.setBorderPainted(false);
        this.setMargin(new Insets(0, 0, 0, 0));
        this.setContentAreaFilled(false);
    }

    /**
     * Create a round button with the icon
     * @param icon the default icon of the button
     */
    public RoundButton(ImageIcon icon) {
        super();
        this.setIcon(icon);
        this.setBorderPainted(false);
        this.setMargin(new Insets(0, 0, 0, 0));
        this.setContentAreaFilled(false);
        // this.addMouseListener(this);

        this.icon = icon;
    }

    /**
     * Create a round button with the 2 icons: default icon and seleted icon
     * @param icon the default icon of the button
     * @param selectedIcon the selected icon of the button
     */
    public RoundButton(ImageIcon icon, ImageIcon selectedIcon) {
        super();
        this.setIcon(icon);
        this.setBorderPainted(false);
        this.setMargin(new Insets(0, 0, 0, 0));
        this.setContentAreaFilled(false);
        this.addMouseListener(this);

        this.icon = icon;
        this.selectedIcon = selectedIcon;
    }

    /**
     * Create a round button with the 2 icons: default icon and seleted icon
     * @param icon the default icon of the button
     * @param selectedIcon the selected icon of the button
     * @param rolloverIcon the rollovered icon of the button
     */
    public RoundButton(ImageIcon icon, ImageIcon selectedIcon, ImageIcon rolloverIcon) {
        super();
        this.setIcon(icon);
        this.setBorderPainted(false);
        this.setMargin(new Insets(0, 0, 0, 0));
        this.setContentAreaFilled(false);
        this.addMouseListener(this);

        this.icon = icon;
        this.selectedIcon = selectedIcon;
        this.rolloverIcon = rolloverIcon;
    }
    /**
     *  Hit detection.
     *
     */
    Shape shape;

    @Override
    /**
     * This function is used to prevent the events when users click on button which
     * it is not area of the button's image
     */
    public boolean contains(int x, int y) {
        // If the button has changed size, make a new shape object.
        if (shape == null || !shape.getBounds().equals(getBounds())) {
            shape = new Ellipse2D.Float(0, 0, this.getWidth(), this.getHeight());
        }

        return shape.contains(x, y);
    }

    public void mouseClicked(MouseEvent e) {
    }

    public void mousePressed(MouseEvent e) {
        this.setIcon(this.selectedIcon);
    }

    public void mouseReleased(MouseEvent e) {
        this.setIcon(this.icon);
    }

    public void mouseEntered(MouseEvent e) {
        if (this.rolloverIcon != null) {
            this.setIcon(this.rolloverIcon);
        } else {
            this.setCursor(new Cursor(Cursor.HAND_CURSOR));
        }
    }

    public void mouseExited(MouseEvent e) {
        if (this.rolloverIcon != null) {
            this.setIcon(this.icon);
        } else {
            this.setCursor(new Cursor(Cursor.DEFAULT_CURSOR));
        }
    }
}
